package com.edu.fafu.studentrepair.fragments;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.edu.fafu.studentrepair.LoginActivity;
import com.edu.fafu.studentrepair.MainActivity;
import com.edu.fafu.studentrepair.R;
import com.edu.fafu.studentrepair.command.Dictionary;
import com.edu.fafu.studentrepair.command.LocalDataStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class HumanInfoFragement extends Fragment {
    private LinearLayout human_pwd_change;
    private LinearLayout human_iden_change;
    private LinearLayout human_log_out;
    private ImageView info_img;
    private TextView info_name;
    private TextView info_state;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.human_info_activity_main,container,false);
        human_pwd_change = view.findViewById(R.id.human_pwd_change);
        human_iden_change = view.findViewById(R.id.human_iden_change);
        human_log_out = view.findViewById(R.id.human_log_out);
        human_pwd_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.setLastfragment(Dictionary.Page.FRAGMENT_PWD_CHANGE);
                mainActivity.switchFragment(Dictionary.Page.FRAGMENT_HUMAN_INFO,Dictionary.Page.FRAGMENT_PWD_CHANGE);
            }
        });
        info_img = view.findViewById(R.id.info_img);
        info_name = view.findViewById(R.id.info_name);
        info_state = view.findViewById(R.id.info_state);
        try {
            String info = LocalDataStream.read(getActivity().openFileInput("data"));
            String state = info.split("_")[3];
            String picurl = info.split("_")[4];
            if(state.equals("1")) info_state.setVisibility(View.INVISIBLE);//没被禁用
            if(!picurl.equals("null")) Glide.with(this).load(picurl).into(info_img);//设置头像
            info_name.setText(info.split("_")[5]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        human_iden_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("是否确认退出");

            builder.setPositiveButton("取消", null);
            builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        FileOutputStream out = getActivity().openFileOutput("data", Context.MODE_PRIVATE);
                        LocalDataStream.writer("",out);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

                    //清空本地账号信息；
                    Intent intent = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
            layoutParams.width = 700;
            dialog.getWindow().setAttributes(layoutParams);

            }
        });
        human_log_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("是否确认退出");

            builder.setPositiveButton("取消", null);
            builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //清空本地账号信息；
                    try {
                        FileOutputStream out = getActivity().openFileOutput("data", Context.MODE_PRIVATE);
                        LocalDataStream.writer("",out);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    getActivity().finish();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
            layoutParams.width = 700;
            dialog.getWindow().setAttributes(layoutParams);

            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
